package com.budwk.app.sqfwV2.generator.propagandaScreening;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.constants.CodeGeneratorConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.dto.dulaying.ScreeningHiddenDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.sqfwV2.enums.PropagandaScreeningEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.lang.util.NutMap;

import java.util.*;
import java.util.stream.Collectors;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class PropagandaScreeningExcelGenerator extends BaseExcelGenerator {

    private String templateName;


    public PropagandaScreeningExcelGenerator() {
    }

    public PropagandaScreeningExcelGenerator(String templateName) {
        this.templateName = templateName;
    }


    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {
        if (StringUtils.isNotBlank(excelGenerateParam.getClientType()) && excelGenerateParam.getClientType().equals(CodeGeneratorConstants.CLIENT_TYPE_ADMIN)) {
            Map<String, Object> map = excelGenerateParam.getExcelConfig().getParam();
            String type = PropagandaScreeningEnum.getName(Integer.valueOf(excelGenerateParam.getExcelConfig().getFileIdentity()));
            SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
            Sys_user sysUser = sysUserService.getUserById(SecurityUtil.getUserId());
            SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
            List<String> ids = new ArrayList<String>();
            List<ExcelRowDataParam> dataList = excelGenerateParam.getDataList();
            dataList.forEach(item -> {
                Map<String, String> businessData = new HashMap<String, String>();
                Sqfw_formSummary_new formSummary = (Sqfw_formSummary_new) item.getCustomerParamMap().get("formSummaryNew");
                ids.add(formSummary.getId());
            });
            List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(
                            Sqfw_formSummary_new::getType, "=", SupportBusinessTypeEnum.PROPAGANDA_SCREENING.getName())
                    .and("id", "IN", ids)
                    .and("param_data->>'type'", "=", type));
            int num = 0;
            if (ObjUtil.isNotEmpty(formSummaryNewList)) {
                listMap.clear();
                for (Sqfw_formSummary_new sqfwFormSummaryNew : formSummaryNewList) {
                    PropagandaScreeningDTO propagandaScreeningDTO = JSONObject.parseObject(sqfwFormSummaryNew.getParamData(), PropagandaScreeningDTO.class);
                    List<ScreeningHiddenDTO> screeningHiddenList = propagandaScreeningDTO.getScreeningHiddenList();
                    for (ScreeningHiddenDTO screeningHiddenDTO : screeningHiddenList) {
                        NutMap nutMap = NutMap.NEW();
                        num = num + 1;
                        nutMap.addv("num", num)
                                .addv("community", sqfwFormSummaryNew.getCommunity())
                                .addv("theme", screeningHiddenDTO.getTheme())
                                .addv("date", DateUtil.formatDate(propagandaScreeningDTO.getDate()))
                                .addv("propagandaObject", propagandaScreeningDTO.getPropagandaObject())
                                .addv("address", propagandaScreeningDTO.getAddress())
                                .addv("phone", propagandaScreeningDTO.getPhone())
                                .addv("otherHiddenDangers", StrUtil.isNotBlank(screeningHiddenDTO.getOtherHiddenDangers()) ? screeningHiddenDTO.getOtherHiddenDangers() : "-")
                                .addv("peersName", propagandaScreeningDTO.getPeersName());
                        if (ObjectUtil.isNotEmpty(screeningHiddenDTO.getWhetherThereIsAnyHiddenDanger())) {
                            nutMap.put("whetherThereIsAnyHiddenDanger", screeningHiddenDTO.getWhetherThereIsAnyHiddenDanger() ? "是" : "否");
                        } else {
                            nutMap.put("whetherThereIsAnyHiddenDanger", null);
                        }
                        if (StrUtil.isNotBlank(propagandaScreeningDTO.getImages())) {
                            nutMap.put("visitPictures", addPictureToByteList(Arrays.asList(propagandaScreeningDTO.getImages().split(",")).get(0), 1));
                        } else {
                            nutMap.put("visitPictures",null );
                        }
                        listMap.add(nutMap);
                    }
                }
            }
        } else {
            Map<String, Object> map = excelGenerateParam.getExcelConfig().getParam();
            String type = PropagandaScreeningEnum.getName(Integer.valueOf(excelGenerateParam.getExcelConfig().getFileIdentity()));
            SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
            Sys_user sysUser = sysUserService.getUserById(SecurityUtil.getUserId());


            SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);

            List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(
                            Sqfw_formSummary_new::getType, "=", SupportBusinessTypeEnum.PROPAGANDA_SCREENING.getName())
                    .and(Sqfw_formSummary_new::getFillDate, "between", new Object[]{map.get("fillStartDate"), map.get("fillEndDate")})
                    .and("param_data->>'type'", "=", type)
                    .and(sysUser.getCommunity().equals("直属") ? "county" : "community", "=", sysUser.getCommunity().equals("直属") ? sysUser.getCounty() : sysUser.getCommunity()));
            int num = 0;
            if (ObjUtil.isNotEmpty(formSummaryNewList)) {
                for (Sqfw_formSummary_new sqfwFormSummaryNew : formSummaryNewList) {
                    PropagandaScreeningDTO propagandaScreeningDTO = JSONObject.parseObject(sqfwFormSummaryNew.getParamData(), PropagandaScreeningDTO.class);
                    List<ScreeningHiddenDTO> screeningHiddenList = propagandaScreeningDTO.getScreeningHiddenList();
                    for (ScreeningHiddenDTO screeningHiddenDTO : screeningHiddenList) {
                        NutMap nutMap = NutMap.NEW();
                        num = num + 1;
                        nutMap.addv("num", num)
                                .addv("community", sqfwFormSummaryNew.getCommunity())
                                .addv("theme", screeningHiddenDTO.getTheme())
                                .addv("date", DateUtil.formatDate(propagandaScreeningDTO.getDate()))
                                .addv("propagandaObject", propagandaScreeningDTO.getPropagandaObject())
                                .addv("address", propagandaScreeningDTO.getAddress())
                                .addv("phone", propagandaScreeningDTO.getPhone())
                                .addv("otherHiddenDangers", StrUtil.isNotBlank(screeningHiddenDTO.getOtherHiddenDangers()) ? screeningHiddenDTO.getOtherHiddenDangers() : "-")
                                .addv("peersName", propagandaScreeningDTO.getPeersName());
                        if (ObjectUtil.isNotEmpty(screeningHiddenDTO.getWhetherThereIsAnyHiddenDanger())) {
                            nutMap.put("whetherThereIsAnyHiddenDanger", screeningHiddenDTO.getWhetherThereIsAnyHiddenDanger() ? "是" : "否");
                        } else {
                            nutMap.put("whetherThereIsAnyHiddenDanger", null);
                        }
                        if (StrUtil.isNotBlank(propagandaScreeningDTO.getImages())) {
                            nutMap.put("visitPictures", addPictureToByteList(Arrays.asList(propagandaScreeningDTO.getImages().split(",")).get(0), 1));
                        } else {
                            nutMap.put("visitPictures", null);
                        }
                        listMap.add(nutMap);
                    }
                }
            }
        }
    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return PropagandaScreeningEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }


}
